package com.glink.manage.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.manage.domain.ConstructionWorkBean;
import com.glink.manage.domain.OrgBean;
import com.glink.manage.domain.PatrolIntelligentEquipChannel;
import com.glink.manage.dto.video.PatrolIntelligentEquipChannelNvrDTO;
import com.glink.manage.mapper.OrgMapper;
import com.glink.manage.mapper.PatrolIntelligentEquipChannelMapper;
import com.glink.manage.service.ConstructionWorkService;
import com.glink.manage.service.OrgService;
import com.glink.manage.service.PatrolIntelligentEquipChannelService;
import com.glink.manage.service.UserService;
import com.glink.manage.vo.video.PatrolIntelligentEquipChannelNvrVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author glink
 * @since 2025/1/9
 */
@Service
public class PatrolIntelligentEquipChannelServiceImpl extends ServiceImpl<PatrolIntelligentEquipChannelMapper, PatrolIntelligentEquipChannel> implements PatrolIntelligentEquipChannelService {

    @Autowired
    private ConstructionWorkService constructionWorkService;

    @Autowired
    private OrgService orgService;

    @Autowired
    private OrgMapper orgMapper;

    @Autowired
    private UserService userService;

    @Override
    public IPage<PatrolIntelligentEquipChannelNvrVO> findPage(PatrolIntelligentEquipChannelNvrDTO dto) throws GeneralSimpleException {
        Page<PatrolIntelligentEquipChannel> page = new Page<>(dto.getPage(), dto.getSize());

        //查询是否具有总部管理员角色
        boolean headOffice = userService.checkHeadOffice();
        List<String> orgIdList = null;
        if (!headOffice) {
            orgIdList = orgService.getDataPermissionOrgId();
            if(CollectionUtils.isEmpty(orgIdList)){
                return new Page<>(dto.getPage(), dto.getSize());
            }
        }
        //当前组织的工程列表
        List<ConstructionWorkBean> constructionWorkBeanList = this.constructionWorkService.lambdaQuery()
                .in(!headOffice, ConstructionWorkBean::getProjectDepartmentObjId, orgIdList)
                .orderByAsc(ConstructionWorkBean::getCreateTime).list();
        if (CollectionUtils.isNotEmpty(constructionWorkBeanList)) {
            List<String> workObjIdList = Lists.newArrayList();
            constructionWorkBeanList.stream().forEach(
                    constructionWorkBean -> {
                        workObjIdList.add(constructionWorkBean.getObjId());
                    }
            );
            return this.baseMapper.findPage(page, dto.getSearchName(), workObjIdList);
        }

        return null;
    }

    @Override
    public IPage<PatrolIntelligentEquipChannelNvrVO> findNoHomeSetChannelListPage(Page<PatrolIntelligentEquipChannel> page,String searchName, List<String> workSiteIds, List<String> homeSetChannelIds) throws GeneralSimpleException {
        return this.baseMapper.findNoHomeSetChannelListPage(page,searchName, workSiteIds,homeSetChannelIds);
    }

    private void listChildOrg(List<String> pidList, List<String> childOrgIdList) {
        LambdaQueryWrapper<OrgBean> orgWrapper = Wrappers.lambdaQuery();
        orgWrapper.in(OrgBean::getPid, pidList);
        List<OrgBean> orgList = orgMapper.selectList(orgWrapper);
        if (CollUtil.isNotEmpty(orgList)) {
            List<String> childIdList = orgList.stream().map(OrgBean::getId).collect(Collectors.toList());
            childOrgIdList.addAll(childIdList);
            listChildOrg(childIdList, childOrgIdList);
        }
    }

}
